package com.antler.smt.portal.service.impl;

import com.antler.smt.entity.PeopleInfoEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.ClassifyInfoMapper;
import com.antler.smt.portal.mapper.PeopleInfoEntityMapper;
import com.antler.smt.request.portal.ResumeReq;
import com.antler.smt.request.portal.SalaryRangeReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.response.portal.ProjectResp;
import com.antler.smt.response.portal.ResumeResp;
import com.antler.smt.service.portal.ClassifyService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangyi
 * @date 2018/9/26 15:20
 */
@Service
@Slf4j
public class ClassifyServiceImpl implements ClassifyService {

    /**
     * 订单薪资范围
     */
    public static  final int ONE_S      = 0;
    public static  final int TWO_S      = 1;
    public static  final int THREE_S    = 2;
    public static  final int FOUR_S     = 3;
    public static  final int FIVE_S     = 4;


    @Autowired
    private ClassifyInfoMapper classifyInfoMapper;

    @Autowired
    private PeopleInfoEntityMapper peopleInfoEntityMapper;

    /**
     * 获取员工的建立简历集合
     *
     * @param resumeReq
     * @return
     */
    @Override
    public Result getPeopleResumes(ResumeReq resumeReq) {
        Result result = null;
        ResumeReq resumeReq1 = setSalaryAndYear(resumeReq);
        Page<ResumeResp> resumeResps =
                classifyInfoMapper.selectPeopleResumes(resumeReq1);
        if(resumeResps != null){
            result = new Result(new PageInfo(resumeResps));
        }else{
            result = new Result(ResultCodeEnum.QUERY_DATA_ERROR,"查询简历库失败！");
            log.error("获取员工的建立简历查询失败");
        }
        return result;
    }

    /**
     * 筛选招聘要求
     *
     * @param resumeReq
     * @return
     */
    @Override
    public Result getRequirements(ResumeReq resumeReq) {
        Result result = null;
        ResumeReq resumeReq1 = setSalaryAndYear(resumeReq);
        Page<ResumeResp> resumeResps =
                classifyInfoMapper.selectRequirements(resumeReq1);

        if(resumeResps != null){
            result = new Result(new PageInfo(resumeResps));
        }else{
            result = new Result(ResultCodeEnum.QUERY_DATA_ERROR,"查询招聘需求失败！");
            log.error("筛选招聘要求查询失败");
        }
        return result;
    }

    /**
     * 筛选项目金额
     *
     * @param salaryRange
     * @return
     */
    @Override
    public Result getProjects(List<Integer> salaryRange) {
        Result result = null;
        SalaryRangeReq salaryRangeReq = new SalaryRangeReq();
        salaryRange.stream()
                .forEach(e -> {
                    Integer salary = e*1000;
                    if(salaryRangeReq.getMinSalary() != null || salaryRangeReq.getMaxSalary() != null){
                        if(salaryRangeReq.getMinSalary() <= salary){
                            salaryRangeReq.setMaxSalary(Long.parseLong(String.valueOf(salary)));
                        }else{
                            salaryRangeReq.setMinSalary(Long.parseLong(String.valueOf(salary)));
                            salaryRangeReq.setMaxSalary(salaryRangeReq.getMinSalary());
                        }
                    }else{
                        salaryRangeReq.setMinSalary(Long.parseLong(String.valueOf(salary)));
                    }
                });
        Page<ProjectResp> projectResps = classifyInfoMapper.selectProjects(salaryRangeReq);
        PageInfo projectPageInfo = null;
        if(projectResps != null){
            projectPageInfo  = new PageInfo(projectResps);
            result = new Result(projectPageInfo);
        }else{
            result = new Result(ResultCodeEnum.QUERY_DATA_ERROR,"查询项目金额失败！");
            log.error("筛选项目金额查询失败");
        }
        return result;
    }

    /**
     * 获取简历详情
     *
     * @param peopleId
     * @return
     */
    @Override
    public Result getResumeDetailed(Long peopleId) {
        PeopleInfoEntity peopleInfoEntity =
                peopleInfoEntityMapper.selectByPrimaryKey(peopleId);
        return new Result(peopleInfoEntity);
    }

    /**
     * 工作年限和工资范围划分
     *
     * @param resumeReq
     * @return
     */
    private ResumeReq setSalaryAndYear(ResumeReq resumeReq){
        //非空处理
        if(resumeReq.getExpectedSalary() == null){
            resumeReq.setMinSalary(new Long(0));
            resumeReq.setMaxSalary(new Long(10000));
        }else {
            switch (resumeReq.getExpectedSalary()){
                case ONE_S:
                    resumeReq.setMinSalary(new Long(0));
                    resumeReq.setMaxSalary(new Long(10000));
                    break;
                case TWO_S:
                    resumeReq.setMinSalary(new Long(0));
                    resumeReq.setMaxSalary(new Long(100));
                    break;
                case THREE_S:
                    resumeReq.setMinSalary(new Long(100));
                    resumeReq.setMaxSalary(new Long(300));
                    break;
                case FOUR_S:
                    resumeReq.setMinSalary(new Long(300));
                    resumeReq.setMaxSalary(new Long(500));
                    break;
                case FIVE_S:
                    resumeReq.setMinSalary(new Long(500));
                    resumeReq.setMaxSalary(new Long(10000));
                    break;
                    default:
                        resumeReq.setMinSalary(new Long(0));
                        resumeReq.setMaxSalary(new Long(10000));
                        break;
            }
        }
        if(resumeReq.getWorkingYears() == null){
            resumeReq.setMinYear(0);
            resumeReq.setMaxYear(11);
        }else {
            switch (resumeReq.getWorkingYears()){
                case ONE_S:
                    resumeReq.setMinYear(0);
                    resumeReq.setMaxYear(11);
                    break;
                case TWO_S:
                    resumeReq.setMinYear(1);
                    resumeReq.setMaxYear(2);
                    break;
                case THREE_S:
                    resumeReq.setMinYear(3);
                    resumeReq.setMaxYear(5);
                    break;
                case FOUR_S:
                    resumeReq.setMinYear(6);
                    resumeReq.setMaxYear(10);
                    break;
                case FIVE_S:
                    resumeReq.setMinYear(11);
                    resumeReq.setMaxYear(11);
                    break;
                    default:
                        resumeReq.setMinYear(0);
                        resumeReq.setMaxYear(11);
                        break;
            }
        }
        return  resumeReq;
    }
}
